<template>
  <div class="unit">
    <el-header>
      <el-input v-model="querryInfo.communityname" placeholder="小区名称" clearable @clear="getUnit()"
        style="width: 200px;"></el-input>
      <el-input v-model="querryInfo.unitname" placeholder="单元名称" clearable @clear="getUnit()"
        style="width: 200px;"></el-input>
      <el-date-picker v-model="querryInfo.createDt" type="datetime" placeholder="选择日期时间" align="right"
        :picker-options="pickerOptions" format="yyyy-MM-dd" value-format="yyyy-MM-dd">
      </el-date-picker>

      <el-select v-model="querryInfo.value" clearable @clear="getUnit()" placeholder="选择状态" style="width: 200px;">
        <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value">
        </el-option>
      </el-select>
      <el-button type="primary" @click="getUnit()" icon="el-icon-search"></el-button>
      <el-button type="success" @click="add()" icon="el-icon-plus">录入</el-button>
      <el-button type="primary" @click="reset()" icon="el-icon-refresh-right">重置</el-button>
    </el-header>
    <el-table ref='multipleTable' :data="unitList" tooltip-effect="dark" style="width: 100%" v-loading="loading">
      <el-table-column label="小区名称" align="center">
        <template slot-scope="scope">{{ scope.row.communityname }}</template>
      </el-table-column>
      <el-table-column label="单元名称" align="center">
        <template slot-scope="scope">{{ scope.row.unitname }}</template>
      </el-table-column>
      <el-table-column label="栋数" align="center">
        <template slot-scope="scope">{{ scope.row.unitnum }}</template>
      </el-table-column>
      <el-table-column label="建设时间" align="center">
        <template slot-scope="scope">{{ formatDateTime(scope.row.createtime) }}</template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">{{ getStatusLabel(scope.row.unitstatus) }}</template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="" style="color: #62a8ea;"
            @click="unitDetail(scope.$index, scope.row)">详情</el-button>
          <el-button size="mini" type="danger" @click="unitDelete(scope.$index, scope.row)">归档</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination style="text-align: right;" :page="querryInfo.currPage" :componentTotal="unitTotal" @changeRows="setRows"
      @changePage="setPage">
    </pagination>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
export default {
  data() {
    return {
      querryInfo: {
        communityname: '',
        unitname: '',
        createDt: '',
        value: '',
        currPage: 1,
        pageNum: 10
      },
      unitForm: {
        unitstatus: '',
      },
      id: '',
      loading: false,
      unitList: [],
      unitTotal: 0,
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date());
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24);
            picker.$emit('pick', date);
          }
        }, {
          text: '一周前',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick',date)
          }
        }]
      },
      statusOptions: [
        { value: '1', label: '已建成' },
        { value: '2', label: '未建成' },
      ]
    };
  },
  computed: {
    getStatusLabel() {
      return (unitstatus) => {
        const status = this.statusOptions.find(option => option.value == unitstatus);
        return status ? status.label : '未知状态';
      };
    }
  },
  components: {
    Pagination
  },
  mounted() {
    this.getUnit();
  },
  methods: {
    // 分页器
    setRows(val) {
      this.querryInfo.pageNum = val;
      this.getUnit();
    },
    setPage(val) {
      this.querryInfo.currPage = val;
      this.getUnit();
    },
    formatDateTime(createtime) {
      if (!createtime) return '';
      const date = new Date(createtime);
      const year = date.getFullYear().toString().slice(-2); // 获取年份的后两位
      const month = String(date.getMonth() + 1).padStart(2, '0'); // 获取月份并补零
      const day = String(date.getDate()).padStart(2, '0'); // 获取日期并补零
      const hours = String(date.getHours()).padStart(2, '0'); // 获取小时并补零
      const minutes = String(date.getMinutes()).padStart(2, '0'); // 获取分钟并补零
      const seconds = String(date.getSeconds()).padStart(2, '0'); // 获取秒并补零
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    },
    // async getUserByToken() {
    //   const { data: res } = await this.axios.get("/user/getUserByToken", {
    //     params: {
    //       token: sessionStorage.token
    //     }
    //   })
    //   console.log(res);
    // },
    async getUnit() {
      this.loading = true;
      const { data: res } = await this.axios.get("/house/searchList", {
        params: this.querryInfo,
        headers: {
          token: sessionStorage.token
        }
      });
      // console.log(res);
      this.unitList = res.data
      this.unitTotal = res.total;
      console.log(this.unitList);
      this.loading = false;
    },
    // clickUnit() {
    //   setTimeout(() => {
    //     this.getUnit();
    //   }, 1000);
    // },
    async add() {
      this.$router.push({ name: 'Add', query: { type: 'ADD' } })
    },
    async unitDetail(index, row) {
      this.id = row.id;
      this.$router.push({ name: 'Add', query: { type: 'DETAIL', detailId: this.id } })
    },
    async unitDelete(index, row) {
      this.id = row.id;
      const { data: res } = await this.axios.get("/house/delunitList", {
        params: {
          id: this.id,
          token: sessionStorage.token
        }
      })
      console.log(res);
      this.$confirm('确认归档该单元？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
        this.getUnit();
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      })
    },
    reset() {
      this.querryInfo.communityname = ''
      this.querryInfo.unitname = ''
      this.querryInfo.createDt = ''
      this.querryInfo.value = ''
      this.getUnit();
    }
  }
}
</script>

<style scoped lang="scss">
.unit {
  margin: 10px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  height: 100%;


  .el-header {
    background-color: #fff;
    margin: 10px;
    line-height: 60px;

    .el-input,
    .el-select {
      margin: 5px 10px;
    }
  }
}
</style>
